/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 wave_stream.cpp

	$Header: /resource_editor/windows_file.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "standard_c_file.h"
#include "assert.h"

// -------------------------------------------------------------
// standard c file access functions wrapper (fopen, etc..)
// -------------------------------------------------------------

t_standard_c_file::t_standard_c_file()
{	
	m_file_state	= k_file_closed;
	m_file_handle	= NULL;
}

t_standard_c_file::~t_standard_c_file()
{
	close();
}

bool t_standard_c_file::open( const char *filename, const char *mode )
{
	close();

	FILE* result = fopen(filename, mode);

	if (result == NULL)
		return false;
	
	m_file_state	= k_file_opened;
	m_file_handle	= result;
	
	return true;
}

bool t_standard_c_file::close( )
{
	if (m_file_state == k_file_opened)
	{
		if (fclose(m_file_handle) != 0) 
			return false;
	}
	
	m_file_state	= k_file_closed;
	m_file_handle	= NULL;

	return true;
}

t_int32		t_standard_c_file::filesize( )
{
	if (m_file_state == k_file_closed)
		return 0;

	fpos_t current_pos = 0;

	if (fgetpos(m_file_handle, &current_pos) != 0)
		return 0;
	
	fseek( m_file_handle, 0, SEEK_END );

	int file_size = ftell( m_file_handle );
	
	if (fsetpos(m_file_handle, &current_pos) != 0)
		return 0;
	
	return file_size;
}

t_int32		t_standard_c_file::seek( long offset, int origin )
{  
	if (m_file_state == k_file_closed)
		return 0;

	return fseek(m_file_handle, offset, origin);
}

t_uint32	t_standard_c_file::read( void *buffer, t_uint32 size, t_uint32 count)
{
	if (m_file_state == k_file_closed)
		return 0;
    
	return fread( buffer,  size,  count , m_file_handle );
}

t_uint32	t_standard_c_file::write( const void *buffer, t_uint32 size, t_uint32 count)
{
	if (m_file_state == k_file_closed)
		return 0;

	return fwrite( buffer,  size,  count , m_file_handle );
}

char *	t_standard_c_file::get_file_handle()
{ 
	//cannot cast FILE* to char* 
	return NULL;
}
